#include "server.h"
#include "user.qpb.h"
#include "friend.qpb.h"
#include "message_storage.qpb.h"
#include "message_transmit.qpb.h"
#include "notify.qpb.h"
#include "file.qpb.h"
#include "speech_recognition.qpb.h"
#include "../ChatClient/utils/log.hpp"
#include "../ChatClient/utils/widgetuitl.hpp"

/* ============================================= *
 *          HttpServer::instance                *
 * ============================================ */
HttpServer* HttpServer::instance = nullptr;

HttpServer::HttpServer() {}

HttpServer* HttpServer::getInstance()
{
    if(!instance) {
        instance = new HttpServer();
    }
    return instance;
}

bool HttpServer::init()
{
    int ret = httpServer.listen(QHostAddress::Any, 8000);

    httpServer.route("/ping", [](const QHttpServerRequest& req) {
        (void)req;
        qDebug() << "[http] 收到 ping 请求.";
        return "pong";
    });

    httpServer.route("/service/user/get_user_info", [this](const QHttpServerRequest& req){
        return this->getUserInfo(req);
    });

    httpServer.route("/service/friend/get_friend_list", [this](const QHttpServerRequest& req){
        return this->getFriendList(req);
    });

    httpServer.route("/service/friend/get_chat_session_list", [this](const QHttpServerRequest& req){
        return this->getChatSessionList(req);
    });

    httpServer.route("/service/friend/get_pending_friend_events", [this](const QHttpServerRequest& req){
        return this->getApplyList(req);
    });

    httpServer.route("/service/message_storage/get_recent", [this](const QHttpServerRequest& req){
        return this->getRecentMessageList(req);
    });

    httpServer.route("/service/message_transmit/new_message", [this](const QHttpServerRequest& req){
        return this->newMessage(req);
    });

    httpServer.route("/service/user/set_nickname", [this](const QHttpServerRequest& req){
        return this->setNickname(req);
    });

    httpServer.route("/service/user/set_description", [this](const QHttpServerRequest& req){
        return this->setDescription(req);
    });

    httpServer.route("/service/user/get_phone_verify_code", [this](const QHttpServerRequest& req){
        return this->getPhoneVerifyCode(req);
    });

    httpServer.route("/service/user/set_phone", [this](const QHttpServerRequest& req){
        return this->setPhone(req);
    });

    httpServer.route("/service/user/set_avatar", [this](const QHttpServerRequest& req){
        return this->setAvatar(req);
    });

    httpServer.route("/service/friend/remove_friend", [this](const QHttpServerRequest& req){
        return this->removeFriend(req);
    });

    httpServer.route("/service/friend/add_friend_apply", [this](const QHttpServerRequest& req){
        return this->addFriendApply(req);
    });

    httpServer.route("/service/friend/add_friend_process", [this](const QHttpServerRequest& req){
        return this->addFriendProcess(req);
    });

    httpServer.route("/service/friend/create_chat_session", [this](const QHttpServerRequest& req){
        return this->createChatSession(req);
    });

    httpServer.route("/service/friend/get_chat_session_member", [this](const QHttpServerRequest& req){
        return this->getSessionMembers(req);
    });

    httpServer.route("/service/friend/search_friend", [this](const QHttpServerRequest& req){
        return this->searchUser(req);
    });

    httpServer.route("/service/message_storage/search_history", [this](const QHttpServerRequest& req){
        return this->searchHisMessage(req);
    });

    httpServer.route("/service/message_storage/get_history", [this](const QHttpServerRequest& req){
        return this->searchHisMessageByTime(req);
    });

    httpServer.route("/service/user/username_login", [this](const QHttpServerRequest& req){
        return this->accountLogin(req);
    });

    httpServer.route("/service/user/phone_login", [this](const QHttpServerRequest& req){
        return this->phoneLogin(req);
    });

    httpServer.route("/service/user/register", [this](const QHttpServerRequest& req){
        return this->userRegister(req);
    });

    httpServer.route("/service/file/get_single_file", [this](const QHttpServerRequest& req){
        return this->getSingleFile(req);
    });

    httpServer.route("/service/speech/recognition", [this](const QHttpServerRequest& req){
        return this->speechRecognition(req);
    });

    return ret == 8000;
}

QHttpServerResponse HttpServer::getUserInfo(const QHttpServerRequest& req)
{
    // 1.提取请求应正文数据, 反序列化
    im_proto::GetUserInfoReq userInfoReq;
    userInfoReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 获取用户信息] requestId: {}, loginSessionId: {}", userInfoReq.requestId(), userInfoReq.sessionId());

    // 2.构建响应数据
    im_proto::GetUserInfoRsp userInfoResp;
    userInfoResp.setRequestId(userInfoReq.requestId());
    userInfoResp.setSuccess(true);
    userInfoResp.setErrmsg("");

    im_proto::UserInfo userInfo;
    userInfo.setUserId("1029");
    userInfo.setNickname("小美");
    userInfo.setDescription("我是小美");
    userInfo.setPhone("1593578642");
    userInfo.setAvatar(utils::FileUtil::loadByteArrayFromFile(":/resource/image/defaultAvatar.png"));
    userInfoResp.setUserInfo(userInfo);

    QByteArray body = userInfoResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getFriendList(const QHttpServerRequest& req)
{
    im_proto::GetFriendListReq friendListReq;
    friendListReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 获取用户信息] requestId: {}, loginSessionId: {}", friendListReq.requestId(), friendListReq.sessionId());

    // 2.构建响应数据
    im_proto::GetFriendListRsp friendListResp;
    friendListResp.setRequestId(friendListReq.requestId());
    friendListResp.setSuccess(true);
    friendListResp.setErrmsg("");

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/defaultAvatar.png");
    for(int i = 0; i < 20; ++i) {
        im_proto::UserInfo userInfo = *createUserInfo(i, avatar);
        friendListResp.friendList().emplace_back(userInfo);
    }

    QByteArray body = friendListResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getChatSessionList(const QHttpServerRequest& req)
{
    im_proto::GetChatSessionListReq chatSessionListReq;
    chatSessionListReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 获取会话信息] requestId: {}, loginSessionId: {}", chatSessionListReq.requestId(), chatSessionListReq.sessionId());

    // 2.构建响应数据
    im_proto::GetChatSessionListRsp chatSessionListResp;
    chatSessionListResp.setRequestId(chatSessionListReq.requestId());
    chatSessionListResp.setSuccess(true);
    chatSessionListResp.setErrmsg("");

    // 构造单聊会话
    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/defaultAvatar.png");
    for(int i = 0; i < 30; ++i) {
        im_proto::ChatSessionInfo chatSessionInfo = *createChatSessionInfo(i, avatar);
        chatSessionListResp.chatSessionInfoList().emplace_back(chatSessionInfo);
    }

    // 构造群聊会话
    const QByteArray gAvatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/groupAvatar.png");
    im_proto::ChatSessionInfo gChatSessionInfo = *createChatSessionInfo(100, gAvatar);
    gChatSessionInfo.setChatSessionName("会话2100");
    gChatSessionInfo.setSingleChatFriendId("");
    chatSessionListResp.chatSessionInfoList().emplace_back(gChatSessionInfo);

    QByteArray body = chatSessionListResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getApplyList(const QHttpServerRequest& req)
{
    im_proto::GetPendingFriendEventListReq applyListReq;
    applyListReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 获取好友申请信息] requestId: {}, loginSessionId: {}", applyListReq.requestId(), applyListReq.sessionId());

    // 2.构建响应数据
    im_proto::GetPendingFriendEventListRsp applyListResp;
    applyListResp.setRequestId(applyListReq.requestId());
    applyListResp.setSuccess(true);
    applyListResp.setErrmsg("");

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/defaultAvatar.png");
    for(int i = 0; i < 30; ++i) {
        im_proto::FriendEvent friendEvent = *createFriendEvent(i, avatar);
        applyListResp.event().emplace_back(friendEvent);
    }

    QByteArray body = applyListResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getRecentMessageList(const QHttpServerRequest& req)
{
    im_proto::GetRecentMsgReq recentMsgReq;
    recentMsgReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 获取最近消息] requestId: {}, loginSessionId: {}", recentMsgReq.requestId(), recentMsgReq.sessionId());

    // 2.构建响应数据
    im_proto::GetRecentMsgRsp recentMsgResp;
    recentMsgResp.setRequestId(recentMsgReq.requestId());
    recentMsgResp.setSuccess(true);
    recentMsgResp.setErrmsg("");

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/defaultAvatar.png");
    for(int i = 0; i < 30; ++i) {
        im_proto::MessageInfo* messageInfo = createTextMessageInfo(i, recentMsgReq.chatSessionId(), avatar);
        recentMsgResp.msgList().emplace_back(*messageInfo);
    }

    im_proto::MessageInfo* imageMsgInfo = createImageMessageInfo(30, recentMsgReq.chatSessionId(), avatar);
    recentMsgResp.msgList().emplace_back(*imageMsgInfo);

    im_proto::MessageInfo* fileMsgInfo = createFileMessageInfo(31, recentMsgReq.chatSessionId(), avatar);
    recentMsgResp.msgList().emplace_back(*fileMsgInfo);

    im_proto::MessageInfo* speechMsg = HttpServer::createSpeechMessageInfo(32, recentMsgReq.chatSessionId(), avatar);
    recentMsgResp.msgList().emplace_back(*speechMsg);

    QByteArray body = recentMsgResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::newMessage(const QHttpServerRequest& req)
{
    im_proto::NewMessageReq newMessageReq;
    newMessageReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 发送消息] requestId: {}, loginSessionId: {}, chatSessionId: {}, messageType: {}",
        newMessageReq.requestId(), newMessageReq.sessionId(), newMessageReq.chatSessionId(), newMessageReq.message().messageType());

    if(newMessageReq.message().messageType() == im_proto::MessageTypeGadget::MessageType::STRING) {
        LOG(LL_DEBUG, "[REQ 消息正文] {},", newMessageReq.message().stringMessage().content());
    }

    // 2.构建响应数据
    im_proto::NewMessageRsp newMessageResp;
    newMessageResp.setRequestId(newMessageReq.requestId());
    newMessageResp.setSuccess(true);
    newMessageResp.setErrmsg("");

    QByteArray body = newMessageResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::setNickname(const QHttpServerRequest& req)
{
    im_proto::SetUserNicknameReq modifyNicknameReq;
    modifyNicknameReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 修改用户昵称] requestId: {}, loginSessionId: {}, nickname: {}",
        modifyNicknameReq.requestId(), modifyNicknameReq.sessionId(), modifyNicknameReq.nickname());

    // 2.构建响应数据
    im_proto::SetUserNicknameRsp modifyNicknameResp;
    modifyNicknameResp.setRequestId(modifyNicknameReq.requestId());
    modifyNicknameResp.setSuccess(true);
    modifyNicknameResp.setErrmsg("");

    QByteArray body = modifyNicknameResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::setDescription(const QHttpServerRequest& req)
{
    im_proto::SetUserDescriptionReq modifyDescriptionReq;
    modifyDescriptionReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 修改用户签名] requestId: {}, loginSessionId: {}, description: {}",
        modifyDescriptionReq.requestId(), modifyDescriptionReq.sessionId(), modifyDescriptionReq.description());

    // 2.构建响应数据
    im_proto::SetUserDescriptionRsp modifyDescriptionResp;
    modifyDescriptionResp.setRequestId(modifyDescriptionReq.requestId());
    modifyDescriptionResp.setSuccess(true);
    modifyDescriptionResp.setErrmsg("");

    QByteArray body = modifyDescriptionResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getPhoneVerifyCode(const QHttpServerRequest &req)
{
    im_proto::PhoneVerifyCodeReq verifyCodeReq;
    verifyCodeReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 获取手机验证码] requestId: {}, phoneNumber: {}", verifyCodeReq.requestId(),  verifyCodeReq.phoneNumber());

    // 2.构建响应数据
    im_proto::PhoneVerifyCodeRsp verifyCodeResp;
    verifyCodeResp.setRequestId(verifyCodeReq.requestId());
    verifyCodeResp.setSuccess(true);
    verifyCodeResp.setErrmsg("");
    verifyCodeResp.setVerifyCodeId("test-147258");

    QByteArray body = verifyCodeResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::setPhone(const QHttpServerRequest &req)
{
    im_proto::SetUserPhoneNumberReq modifyPhoneReq;
    modifyPhoneReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 修改用户手机号] requestId: {}, loginSessionId: {}, phoneNumber: {}, verifyCodeId: {}, verifyCode: {}, ",
        modifyPhoneReq.requestId(), modifyPhoneReq.sessionId(), modifyPhoneReq.phoneNumber(),
        modifyPhoneReq.phoneVerifyCodeId(), modifyPhoneReq.phoneVerifyCode());

    // 2.构建响应数据
    im_proto::SetUserPhoneNumberRsp modifyPhoneResp;
    modifyPhoneResp.setRequestId(modifyPhoneReq.requestId());
    modifyPhoneResp.setSuccess(true);
    modifyPhoneResp.setErrmsg("");

    QByteArray body = modifyPhoneResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::setAvatar(const QHttpServerRequest &req)
{
    im_proto::SetUserAvatarReq modifyAvatarReq;
    modifyAvatarReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 修改用户头像] requestId: {}, loginSessionId: {}", modifyAvatarReq.requestId(), modifyAvatarReq.sessionId());

    // 2.构建响应数据
    im_proto::SetUserAvatarRsp modifyAvatarResp;
    modifyAvatarResp.setRequestId(modifyAvatarReq.requestId());
    modifyAvatarResp.setSuccess(true);
    modifyAvatarResp.setErrmsg("");

    QByteArray body = modifyAvatarResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::removeFriend(const QHttpServerRequest &req)
{
    im_proto::FriendRemoveReq removeFriendReq;
    removeFriendReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 删除好友] requestId: {}, loginSessionId: {}, userID: {}",
        removeFriendReq.requestId(), removeFriendReq.sessionId(), removeFriendReq.peerId());

    // 2.构建响应数据
    im_proto::FriendRemoveRsp removeFriendResp;
    removeFriendResp.setRequestId(removeFriendReq.requestId());
    removeFriendResp.setSuccess(true);
    removeFriendResp.setErrmsg("");

    QByteArray body = removeFriendResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::addFriendApply(const QHttpServerRequest &req)
{
    im_proto::FriendAddReq addFriendReq;
    addFriendReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 申请添加好友] requestId: {}, loginSessionId: {}, respondentId: {}",
        addFriendReq.requestId(), addFriendReq.sessionId(), addFriendReq.respondentId());

    // 2.构建响应数据
    im_proto::FriendAddRsp addFriendResp;
    addFriendResp.setRequestId(addFriendReq.requestId());
    addFriendResp.setSuccess(true);
    addFriendResp.setErrmsg("");

    QByteArray body = addFriendResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::addFriendProcess(const QHttpServerRequest &req)
{
    im_proto::FriendAddProcessReq addFriendProcessReq;
    addFriendProcessReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 添加好友处理] requestId: {}, loginSessionId: {}, agree: {}, applyUserId: {}",
        addFriendProcessReq.requestId(), addFriendProcessReq.sessionId(), addFriendProcessReq.agree(), addFriendProcessReq.applyUserId());

    // 2.构建响应数据
    im_proto::FriendAddProcessRsp addFriendProcessResp;
    addFriendProcessResp.setRequestId(addFriendProcessReq.requestId());
    addFriendProcessResp.setSuccess(true);
    addFriendProcessResp.setErrmsg("");

    QByteArray body = addFriendProcessResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::createChatSession(const QHttpServerRequest &req)
{
    im_proto::ChatSessionCreateReq chatSessionCreateReq;
    chatSessionCreateReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 创建会话] requestId: {}, loginSessionId: {}, memberList: {}",
        chatSessionCreateReq.requestId(), chatSessionCreateReq.sessionId(), chatSessionCreateReq.memberIdList());

    // 2.构建响应数据
    im_proto::ChatSessionCreateRsp chatSessionCreateRspResp;
    chatSessionCreateRspResp.setRequestId(chatSessionCreateReq.requestId());
    chatSessionCreateRspResp.setSuccess(true);
    chatSessionCreateRspResp.setErrmsg("");

    QByteArray body = chatSessionCreateRspResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getSessionMembers(const QHttpServerRequest& req)
{
    im_proto::GetChatSessionMemberReq getChatSessionMemberReq;
    getChatSessionMemberReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 获取会话成员列表] requestId: {}, loginSessionId: {}",
        getChatSessionMemberReq.requestId(), getChatSessionMemberReq.sessionId());

    // 2.构建响应数据
    im_proto::GetChatSessionMemberRsp getChatSessionMemberResp;
    getChatSessionMemberResp.setRequestId(getChatSessionMemberReq.requestId());
    getChatSessionMemberResp.setSuccess(true);
    getChatSessionMemberResp.setErrmsg("");

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/defaultAvatar.png");
    for(int i = 0; i < 30; ++i) {
        im_proto::UserInfo* userInfo = HttpServer::createUserInfo(i, avatar);
        getChatSessionMemberResp.memberInfoList().emplace_back(*userInfo);
    }
    getChatSessionMemberResp.memberInfoList().back().setNickname("超级无敌小美");

    QByteArray body = getChatSessionMemberResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::searchUser(const QHttpServerRequest &req)
{
    im_proto::FriendSearchReq userSearchReq;
    userSearchReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 搜索用户] requestId: {}, loginSessionId: {}, searchKey",
        userSearchReq.requestId(), userSearchReq.sessionId(), userSearchReq.searchKey());

    // 2.构建响应数据
    im_proto::FriendSearchRsp userSearchResp;
    userSearchResp.setRequestId(userSearchReq.requestId());
    userSearchResp.setSuccess(true);
    userSearchResp.setErrmsg("");

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/girl.jpg");
    for(int i = 0; i < 30; ++i) {
        im_proto::UserInfo* userInfo = HttpServer::createUserInfo(i, avatar);
        userSearchResp.userInfo().emplace_back(*userInfo);
    }

    QByteArray body = userSearchResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::searchHisMessage(const QHttpServerRequest& req)
{
    im_proto::MsgSearchReq hisMsgSearchReq;
    hisMsgSearchReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 关键词搜索历史消息] requestId: {}, loginSessionId: {}, chatSessionId: {}, searchKey",
        hisMsgSearchReq.requestId(), hisMsgSearchReq.sessionId(), hisMsgSearchReq.chatSessionId(), hisMsgSearchReq.searchKey());

    // 2.构建响应数据
    im_proto::MsgSearchRsp hisMsgSearchResp;
    hisMsgSearchResp.setRequestId(hisMsgSearchReq.requestId());
    hisMsgSearchResp.setSuccess(true);
    hisMsgSearchResp.setErrmsg("");

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/girl.jpg");
    for(int i = 0; i < 30; ++i) {
        im_proto::MessageInfo* msg = HttpServer::createTextMessageInfo(i, hisMsgSearchReq.chatSessionId(), avatar);
        hisMsgSearchResp.msgList().emplace_back(*msg);
    }

    im_proto::MessageInfo* imageMsg = HttpServer::createImageMessageInfo(30, hisMsgSearchReq.chatSessionId(), avatar);
    hisMsgSearchResp.msgList().emplace_back(*imageMsg);

    im_proto::MessageInfo* fileMsg = HttpServer::createFileMessageInfo(31, hisMsgSearchReq.chatSessionId(), avatar);
    hisMsgSearchResp.msgList().emplace_back(*fileMsg);

    im_proto::MessageInfo* speechMsg = HttpServer::createSpeechMessageInfo(32, hisMsgSearchReq.chatSessionId(), avatar);
    hisMsgSearchResp.msgList().emplace_back(*speechMsg);

    QByteArray body = hisMsgSearchResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::searchHisMessageByTime(const QHttpServerRequest& req)
{
    im_proto::GetHistoryMsgReq hisMsgSearchByTimeReq;
    hisMsgSearchByTimeReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 时间搜索历史消息] requestId: {}, loginSessionId: {}, chatSessionId: {}, beginTime: {}, endTime: {}",
        hisMsgSearchByTimeReq.requestId(), hisMsgSearchByTimeReq.sessionId(),
        hisMsgSearchByTimeReq.chatSessionId(), hisMsgSearchByTimeReq.startTime(), hisMsgSearchByTimeReq.overTime());

    // 2.构建响应数据
    im_proto::GetHistoryMsgRsp hisMsgSearchByTimeResp;
    hisMsgSearchByTimeResp.setRequestId(hisMsgSearchByTimeReq.requestId());
    hisMsgSearchByTimeResp.setSuccess(true);
    hisMsgSearchByTimeResp.setErrmsg("");

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/girl.jpg");
    for(int i = 0; i < 30; ++i) {
        im_proto::MessageInfo* msg = HttpServer::createTextMessageInfo(i, hisMsgSearchByTimeReq.chatSessionId(), avatar);
        hisMsgSearchByTimeResp.msgList().emplace_back(*msg);
    }

    QByteArray body = hisMsgSearchByTimeResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::accountLogin(const QHttpServerRequest& req)
{
    im_proto::AccountLoginReq accountLoginReq;
    accountLoginReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 用户名登录] requestId: {}, username: {}, password: {}",
        accountLoginReq.requestId(), accountLoginReq.nickname(), accountLoginReq.password());

    // 2.构建响应数据
    im_proto::AccountLoginRsp accountLoginResp;
    accountLoginResp.setRequestId(accountLoginReq.requestId());
    accountLoginResp.setSuccess(true);
    accountLoginResp.setErrmsg("");
    accountLoginResp.setLoginSessionId("testLoginSessionId");

    QByteArray body = accountLoginResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::phoneLogin(const QHttpServerRequest &req)
{
    im_proto::PhoneLoginReq phoneLoginReq;
    phoneLoginReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 手机号登录] requestId: {}, phone: {}, verifyCodeId: {}, verifyCode: {}",
        phoneLoginReq.requestId(), phoneLoginReq.phoneNumber(), phoneLoginReq.verifyCodeId(), phoneLoginReq.verifyCode());

    // 2.构建响应数据
    im_proto::PhoneLoginRsp phoneLoginResp;
    phoneLoginResp.setRequestId(phoneLoginReq.requestId());
    phoneLoginResp.setSuccess(true);
    phoneLoginResp.setErrmsg("");
    phoneLoginResp.setLoginSessionId("testLoginSessionId");

    QByteArray body = phoneLoginResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::userRegister(const QHttpServerRequest &req)
{
    im_proto::UserRegisterReq userRegisterReq;
    userRegisterReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 用户注册] requestId: {}, username: {}, password: {}, phone: {}, verifyCodeId: {}, verifyCode: {}",
        userRegisterReq.requestId(), userRegisterReq.nickname(), userRegisterReq.password(),
        userRegisterReq.phoneNumber(), userRegisterReq.verifyCodeId(), userRegisterReq.verifyCode());

    // 2.构建响应数据
    im_proto::UserRegisterRsp userRegisterResp;
    userRegisterResp.setRequestId(userRegisterReq.requestId());
    userRegisterResp.setSuccess(true);
    userRegisterResp.setErrmsg("");

    QByteArray body = userRegisterResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getSingleFile(const QHttpServerRequest &req)
{
    im_proto::GetSingleFileReq getSingleFileReq;
    getSingleFileReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 获取单个文件] requestId: {}, sessionId: {}, fileId: {}",
        getSingleFileReq.requestId(), getSingleFileReq.sessionId(), getSingleFileReq.fileId());

    // 2.构建响应数据
    im_proto::GetSingleFileRsp getSingleFileResp;
    getSingleFileResp.setRequestId(getSingleFileReq.requestId());
    getSingleFileResp.setSuccess(true);
    getSingleFileResp.setErrmsg("");

    im_proto::FileDownloadData fileData;
    fileData.setFileId(getSingleFileReq.fileId());
    if (getSingleFileReq.fileId() == "imageFileId") {
        fileData.setFileContent(utils::FileUtil::loadByteArrayFromFile(":/resource/image/test_his.jpg"));
    } else if (getSingleFileReq.fileId() == "fileFileId") {
        fileData.setFileContent(utils::FileUtil::loadByteArrayFromFile(":/resource/file/test.txt"));
    } else if (getSingleFileReq.fileId() == "speechFileId") {
       fileData.setFileContent(utils::FileUtil::loadByteArrayFromFile(":/resource/file/speech.pcm"));
    } else {
        getSingleFileResp.setSuccess(false);
        getSingleFileResp.setErrmsg("无法识别 fileId");
    }
    getSingleFileResp.setFileData(fileData);

    QByteArray body = getSingleFileResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::speechRecognition(const QHttpServerRequest &req)
{
    im_proto::SpeechRecognitionReq speechRecognitionReq;
    speechRecognitionReq.deserialize(&serializer, req.body());
    LOG(LL_DEBUG, "[REQ 语音转文字] requestId: {}, loginSessionId: {}",
        speechRecognitionReq.requestId(), speechRecognitionReq.sessionId());

    // 2.构建响应数据
    im_proto::SpeechRecognitionRsp speechRecognitionResp;
    speechRecognitionResp.setRequestId(speechRecognitionReq.requestId());
    speechRecognitionResp.setSuccess(true);
    speechRecognitionResp.setErrmsg("");
    speechRecognitionResp.setRecognitionResult("我是奶浓; 我才是奶浓！！！");

    QByteArray body = speechRecognitionResp.serialize(&serializer);

    // 3.构造http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type", "application/x-protobuf");
    return httpResp;
}

im_proto::UserInfo* HttpServer::createUserInfo(int idx, const QByteArray& avatar)
{
    im_proto::UserInfo* userInfo = new im_proto::UserInfo();
    userInfo->setUserId(QString::number(idx + 1000));
    userInfo->setNickname("小美" + QString::number(idx));
    userInfo->setDescription("我是小美" + QString::number(idx));
    userInfo->setPhone("1593578642");
    userInfo->setAvatar(avatar);
    return userInfo;
}

im_proto::ChatSessionInfo* HttpServer::createChatSessionInfo(int idx, const QByteArray& avatar)
{
    im_proto::ChatSessionInfo* chatSessionInfo = new im_proto::ChatSessionInfo();
    chatSessionInfo->setChatSessionId(QString::number(idx + 2000));
    chatSessionInfo->setChatSessionName("小美" + QString::number(idx));
    chatSessionInfo->setSingleChatFriendId(QString::number(1000 + idx));
    chatSessionInfo->setAvatar(avatar);

    im_proto::MessageInfo* messageInfo = createTextMessageInfo(idx, chatSessionInfo->chatSessionId(), avatar);
    chatSessionInfo->setPrevMessage(*messageInfo);

    return chatSessionInfo;
}

im_proto::MessageInfo* HttpServer::createTextMessageInfo(int idx, const QString& chatSessionId, const QByteArray& avatar)
{
    im_proto::MessageInfo* messageInfo = new im_proto::MessageInfo();
    messageInfo->setMessageId(QString::number(3000 + idx));
    messageInfo->setChatSessionId(chatSessionId);
    messageInfo->setTimestamp(utils::TimeUtil::getNowTime());
    messageInfo->setSender(*(createUserInfo(idx, avatar)));

    im_proto::StringMessageInfo stringMessageInfo;
    stringMessageInfo.setContent("Hello, everybody, you fine or no ?! I'm xiaomei!");

    im_proto::MessageContent messageContent;
    messageContent.setMessageType(im_proto::MessageTypeGadget::STRING);
    messageContent.setStringMessage(stringMessageInfo);

    messageInfo->setMessage(messageContent);
    return messageInfo;
}

im_proto::MessageInfo* HttpServer::createImageMessageInfo(int idx, const QString& chatSessionId, const QByteArray& avatar)
{
    im_proto::MessageInfo* messageInfo = new im_proto::MessageInfo();
    messageInfo->setMessageId(QString::number(4000 + idx));
    messageInfo->setChatSessionId(chatSessionId);
    messageInfo->setTimestamp(utils::TimeUtil::getNowTime());
    messageInfo->setSender(*(createUserInfo(idx, avatar)));

    im_proto::ImageMessageInfo imageMessageInfo;
    imageMessageInfo.setFileId("imageFileId");
    imageMessageInfo.setImageContent("");

    im_proto::MessageContent messageContent;
    messageContent.setMessageType(im_proto::MessageTypeGadget::IMAGE);
    messageContent.setImageMessage(imageMessageInfo);

    messageInfo->setMessage(messageContent);
    return messageInfo;
}

im_proto::MessageInfo* HttpServer::createFileMessageInfo(int idx, const QString& chatSessionId, const QByteArray& avatar)
{
    im_proto::MessageInfo* messageInfo = new im_proto::MessageInfo();
    messageInfo->setMessageId(QString::number(6000 + idx));
    messageInfo->setChatSessionId(chatSessionId);
    messageInfo->setTimestamp(utils::TimeUtil::getNowTime());
    messageInfo->setSender(*(createUserInfo(idx, avatar)));

    im_proto::FileMessageInfo fileMessageInfo;
    fileMessageInfo.setFileId("fileFileId");
    fileMessageInfo.setFileName("test.txt");
    fileMessageInfo.setFileSize(0);

    im_proto::MessageContent messageContent;
    messageContent.setMessageType(im_proto::MessageTypeGadget::FILE);
    messageContent.setFileMessage(fileMessageInfo);

    messageInfo->setMessage(messageContent);
    return messageInfo;
}

im_proto::MessageInfo* HttpServer::createSpeechMessageInfo(int idx, const QString& chatSessionId, const QByteArray& avatar)
{
    im_proto::MessageInfo* messageInfo = new im_proto::MessageInfo();
    messageInfo->setMessageId(QString::number(7000 + idx));
    messageInfo->setChatSessionId(chatSessionId);
    messageInfo->setTimestamp(utils::TimeUtil::getNowTime());
    messageInfo->setSender(*(createUserInfo(idx, avatar)));

    im_proto::SpeechMessageInfo speechMessageInfo;
    speechMessageInfo.setFileId("speechFileId");

    im_proto::MessageContent messageContent;
    messageContent.setMessageType(im_proto::MessageTypeGadget::SPEECH);
    messageContent.setSpeechMessage(speechMessageInfo);

    messageInfo->setMessage(messageContent);
    return messageInfo;
}


im_proto::FriendEvent* HttpServer::createFriendEvent(int idx, const QByteArray& avatar)
{
    im_proto::FriendEvent* friendEvent = new im_proto::FriendEvent();
    friendEvent->setEventId("event" + QString::number(idx));
    friendEvent->setSender(*(createUserInfo(idx, avatar)));
    return friendEvent;
}



/* =============================================== *
 *          WebSocketServer::instance              *
 * =============================================== */
WebSocketServer* WebSocketServer::instance = nullptr;

WebSocketServer::WebSocketServer()
    : websocketServer("Qt WebSocket Server", QWebSocketServer::NonSecureMode)
{}

WebSocketServer* WebSocketServer::getInstance()
{
    if(!instance) {
        instance = new WebSocketServer();
    }
    return instance;
}

bool WebSocketServer::init()
{
    bool ok = websocketServer.listen(QHostAddress::Any, 8001);

    connect(&websocketServer, &QWebSocketServer::newConnection, this, [=](){
        socket = websocketServer.nextPendingConnection();   // 获取此次连接通道的socketfd
        qDebug() << "[websocket] new Connection";
        socket->sendTextMessage("2025-08-25");

        connect(socket, &QWebSocket::disconnected, this, [=](){
            qDebug() << "[websocket] Disconnected";
            disconnect(this, &WebSocketServer::sendTextResp, this, nullptr);    // 断开信号槽关联
            disconnect(this, &WebSocketServer::sendImageResp, this, nullptr);
            disconnect(this, &WebSocketServer::sendFileResp, this, nullptr);
            disconnect(this, &WebSocketServer::sendSpeechResp, this, nullptr);
            disconnect(this, &WebSocketServer::sendFriendRemove, this, nullptr);
            disconnect(this, &WebSocketServer::sendFriendApply, this, nullptr);
            disconnect(this, &WebSocketServer::sendAFriendApplyProcess, this, nullptr);
            disconnect(this, &WebSocketServer::sendCreateChatSession, this, nullptr);
            socket->deleteLater();
        });

        connect(socket, &QWebSocket::errorOccurred, this, [=](QAbstractSocket::SocketError error){
            qDebug() << "[websocket] error: " << error;
        });

        connect(socket, &QWebSocket::textMessageReceived, this, [=](const QString& message){
            qDebug() << "[websocket] received string message: " << message;
        });

        connect(socket, &QWebSocket::binaryMessageReceived, this, [=](const QString& message){
            qDebug() << "[websocket] received binary message: " << message;
            socket->sendBinaryMessage("身份验证成功");
        });

        connect(this, &WebSocketServer::sendTextResp, this, &WebSocketServer::sendTextRespHandle);

        connect(this, &WebSocketServer::sendImageResp, this, &WebSocketServer::sendImageRespHandle);

        connect(this, &WebSocketServer::sendFileResp, this, &WebSocketServer::sendFileRespHandle);

        connect(this, &WebSocketServer::sendSpeechResp, this, &WebSocketServer::sendSpeechRespHandle);

        connect(this, &WebSocketServer::sendFriendRemove, this, &WebSocketServer::sendFriendRemoveHandle);

        connect(this, &WebSocketServer::sendFriendApply, this, &WebSocketServer::sendFriendApplyHandle);

        connect(this, &WebSocketServer::sendAFriendApplyProcess, this, &WebSocketServer::sendAFriendApplyProcessHandle);

        connect(this, &WebSocketServer::sendCreateChatSession, this, &WebSocketServer::sendCreateChatSessionHandle);

    });

    return ok;
}

void WebSocketServer::sendTextRespHandle()
{
    if(!socket || !socket->isValid()) {
        qDebug() << "socket 无效!";
        return;
    }

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/girl.jpg");
    im_proto::MessageInfo* messageInfo = HttpServer::createTextMessageInfo(messageIdx++, "2000", avatar);

    im_proto::NotifyNewMessage notifyNewMessage;
    notifyNewMessage.setMessageInfo(*messageInfo);

    im_proto::NotifyMessage notifyMessage;
    notifyMessage.setNotifyEventId("");
    notifyMessage.setNotifyType(im_proto::NotifyTypeGadget::CHAT_MESSAGE_NOTIFY);
    notifyMessage.setNewMessageInfo(notifyNewMessage);

    QByteArray body = notifyMessage.serialize(&serializer);

    socket->sendBinaryMessage(body);

    qDebug() << "发送文本消息响应: " << messageInfo->message().stringMessage().content();
}

void WebSocketServer::sendImageRespHandle()
{
    if(!socket || !socket->isValid()) {
        qDebug() << "socket 无效!";
        return;
    }

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/girl.jpg");
    im_proto::MessageInfo* messageInfo = HttpServer::createImageMessageInfo(messageIdx++, "2000", avatar);

    im_proto::NotifyNewMessage notifyNewMessage;
    notifyNewMessage.setMessageInfo(*messageInfo);

    im_proto::NotifyMessage notifyMessage;
    notifyMessage.setNotifyEventId("");
    notifyMessage.setNotifyType(im_proto::NotifyTypeGadget::CHAT_MESSAGE_NOTIFY);
    notifyMessage.setNewMessageInfo(notifyNewMessage);

    QByteArray body = notifyMessage.serialize(&serializer);

    socket->sendBinaryMessage(body);

    qDebug() << "发送图片消息响应";
}

void WebSocketServer::sendFileRespHandle()
{
    if(!socket || !socket->isValid()) {
        qDebug() << "socket 无效!";
        return;
    }

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/girl.jpg");
    im_proto::MessageInfo* messageInfo = HttpServer::createFileMessageInfo(messageIdx++, "2000", avatar);

    im_proto::NotifyNewMessage notifyNewMessage;
    notifyNewMessage.setMessageInfo(*messageInfo);

    im_proto::NotifyMessage notifyMessage;
    notifyMessage.setNotifyEventId("");
    notifyMessage.setNotifyType(im_proto::NotifyTypeGadget::CHAT_MESSAGE_NOTIFY);
    notifyMessage.setNewMessageInfo(notifyNewMessage);

    QByteArray body = notifyMessage.serialize(&serializer);

    socket->sendBinaryMessage(body);

    qDebug() << "发送文件消息响应";
}

void WebSocketServer::sendSpeechRespHandle()
{
    if(!socket || !socket->isValid()) {
        qDebug() << "socket 无效!";
        return;
    }

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/girl.jpg");
    im_proto::MessageInfo* messageInfo = HttpServer::createSpeechMessageInfo(messageIdx++, "2000", avatar);

    im_proto::NotifyNewMessage notifyNewMessage;
    notifyNewMessage.setMessageInfo(*messageInfo);

    im_proto::NotifyMessage notifyMessage;
    notifyMessage.setNotifyEventId("");
    notifyMessage.setNotifyType(im_proto::NotifyTypeGadget::CHAT_MESSAGE_NOTIFY);
    notifyMessage.setNewMessageInfo(notifyNewMessage);

    QByteArray body = notifyMessage.serialize(&serializer);

    socket->sendBinaryMessage(body);

    qDebug() << "发送文件消息响应";
}

void WebSocketServer::sendFriendRemoveHandle()
{
    if(!socket || !socket->isValid()) {
        qDebug() << "socket 无效!";
        return;
    }

    im_proto::NotifyFriendRemove notifyFriendRemove;
    notifyFriendRemove.setUserId("1000");

    im_proto::NotifyMessage notifyMessage;
    notifyMessage.setNotifyEventId("");
    notifyMessage.setNotifyType(im_proto::NotifyTypeGadget::FRIEND_REMOVE_NOTIFY);
    notifyMessage.setFriendRemove(notifyFriendRemove);

    QByteArray body = notifyMessage.serialize(&serializer);

    socket->sendBinaryMessage(body);

    qDebug() << "通知好友删除响应::userID: " << notifyFriendRemove.userId();
}

void WebSocketServer::sendFriendApplyHandle()
{
    if(!socket || !socket->isValid()) {
        qDebug() << "socket 无效!";
        return;
    }

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/girl.jpg");
    im_proto::UserInfo* userInfo = HttpServer::createUserInfo(31, avatar);

    im_proto::NotifyFriendAddApply notifyFriendApply;
    notifyFriendApply.setUserInfo(*userInfo);

    im_proto::NotifyMessage notifyMessage;
    notifyMessage.setNotifyEventId("");
    notifyMessage.setNotifyType(im_proto::NotifyTypeGadget::FRIEND_ADD_APPLY_NOTIFY);
    notifyMessage.setFriendAddApply(notifyFriendApply);

    QByteArray body = notifyMessage.serialize(&serializer);

    socket->sendBinaryMessage(body);

    qDebug() << "通知新的好友申请::userID: " << userInfo->userId();
}

void WebSocketServer::sendAFriendApplyProcessHandle(bool agree)
{
    if(!socket || !socket->isValid()) {
        qDebug() << "socket 无效!";
        return;
    }

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/girl.jpg");
    im_proto::UserInfo* userInfo = HttpServer::createUserInfo(32, avatar);

    im_proto::NotifyFriendAddProcess notifyFriendAddProcess;
    notifyFriendAddProcess.setAgree(agree);
    notifyFriendAddProcess.setUserInfo(*userInfo);

    im_proto::NotifyMessage notifyMessage;
    notifyMessage.setNotifyEventId("");
    notifyMessage.setNotifyType(im_proto::NotifyTypeGadget::FRIEND_ADD_PROCESS_NOTIFY);
    notifyMessage.setFriendProcessResult(notifyFriendAddProcess);

    QByteArray body = notifyMessage.serialize(&serializer);

    socket->sendBinaryMessage(body);

    qDebug() << "通知好友的申请结果::userID: " << userInfo->userId() <<", agree: " << agree;
}

void WebSocketServer::sendCreateChatSessionHandle()
{
    if(!socket || !socket->isValid()) {
        qDebug() << "socket 无效!";
        return;
    }

    const QByteArray avatar = utils::FileUtil::loadByteArrayFromFile(":/resource/image/girl.jpg");
    im_proto::ChatSessionInfo* chatSessionInfo = HttpServer::createChatSessionInfo(147, avatar);
    chatSessionInfo->setSingleChatFriendId("");
    chatSessionInfo->setChatSessionName("新的群聊");
    chatSessionInfo->setAvatar(utils::FileUtil::loadByteArrayFromFile(":/resource/image/groupAvatar.png"));

    im_proto::NotifyNewChatSession notifyNewChatSession;
    notifyNewChatSession.setChatSessionInfo(*chatSessionInfo);

    im_proto::NotifyMessage notifyMessage;
    notifyMessage.setNotifyEventId("");
    notifyMessage.setNotifyType(im_proto::NotifyTypeGadget::CHAT_SESSION_CREATE_NOTIFY);
    notifyMessage.setNewChatSessionInfo(notifyNewChatSession);

    QByteArray body = notifyMessage.serialize(&serializer);

    socket->sendBinaryMessage(body);

    qDebug() << "通知新的会话创建::userID: " << chatSessionInfo->chatSessionId();
}

